package com.dzj.fishingcommunity.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dzj.fishingcommunity.common.ResponseResult;
import com.dzj.fishingcommunity.model.Product;
import com.dzj.fishingcommunity.service.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/products")
@Tag(name = "商品管理", description = "商品管理相关的接口")
public class ProductController {

    @Autowired
    private ProductService productService;

    @Operation(summary = "获取所有商品")
    @GetMapping
    public ResponseResult<List<Product>> getAllProducts(
            @Parameter(description = "当前页码", required = false) @RequestParam(defaultValue = "1") int current,
            @Parameter(description = "每页大小", required = false) @RequestParam(defaultValue = "10") int size) {
        Page<Product> page = new Page<>(current, size);
        Page<Product> productPage = productService.page(page);
        return ResponseResult.success(productPage.getRecords());
    }

    @Operation(summary = "根据ID获取商品")
    @GetMapping("/{id}")
    public ResponseResult<Product> getProductById(@PathVariable Long id) {
        Product product = productService.getById(id);
        return ResponseResult.success(product);
    }

    @Operation(summary = "新增商品")
    @PostMapping
    public ResponseResult<Product> addProduct(@RequestBody Product product) {
        productService.save(product);
        return ResponseResult.success(product);
    }

    @Operation(summary = "更新商品")
    @PutMapping("/{id}")
    public ResponseResult<Product> updateProduct(@PathVariable Long id, @RequestBody Product product) {
        product.setId(id);
        productService.updateById(product);
        return ResponseResult.success(product);
    }

    @Operation(summary = "删除商品")
    @DeleteMapping("/{id}")
    public ResponseResult<Void> deleteProduct(@PathVariable Long id) {
        productService.removeById(id);
        return ResponseResult.success(null);
    }
}